#include <mutex>
#include <map>
#include "clcomcommand.h"

static mutex s_lock;
static map<qint64, CLComCommand*> s_mapCMDs;

CLComCommand* CCommandFactory::Allocate()
{
	lock_guard<mutex> lock(s_lock);

    CLComCommand* pcmd = nullptr;

	auto it = s_mapCMDs.begin();
	if (it == s_mapCMDs.end())
	{
        pcmd = new CLComCommand;
	}
	else
	{
		pcmd = it->second;

		s_mapCMDs.erase(it);
	}

	return pcmd;
}

void CCommandFactory::Giveback(CLComCommand* &pCMD)
{
	lock_guard<mutex> lock(s_lock);

    qint64 id = (qint64)pCMD;

	s_mapCMDs[id] = pCMD;

	pCMD = nullptr;
}
